MyST Lab 01

Laboratorio 02



EFRAIN GÓMEZ TAPIA

IF721112

Junio,2022 | Repository: https://github.com/Efragtapia/V2022_MyST.git


Microestructuras y Sistemas de Trading

Laboratorio 02



Abstract

A state price for a given state at a given future point in time indicates how much investors are willing to sacrifice today in return for an extra payment of one unit in that future state. Presumably investors will value a given payment in a given state the same no matter which asset the payment comes from. Therefore state prices are valid for all assets. The value of any specific asset is determined by the general state prices in the market and the state-contingent future payments of the asset. Modern asset pricing theory is based on models of the possible states and the associated state prices.

For potential investors the important characteristics of a financial asset or any other investment opportunity is its current price and its future payments which the investor will be entitled to if she buys the asset.

The roll model allows one to estimate the bid-ask spread from observed transaction prices alone, without information on the underlying bid-ask price quotes and the order flow. The spread represents a potential profit for the market maker handling the transaction, and is a major part of the transaction cost facing investors.

These two models are an essential tool for practices such as high frequency trading.


1. Introduction


In this document we will see the behavior of these two models, specifically within the world of cryptocurrencies, as well as their visualizations.


2. Install/Load Packages and Depedencies


2.1 Python Packages

In order to run this notebook, it is necessary to have installed and/or have the requirements.txt file with the following:

  • pandas>=1.1.0
  • numpy>=1.19.1
  • jupyter>=1.0.0
  • chart_studio>=1.1
  • plotly>=4.14
  • ipykernel
  • plotly

2.2 Files Dependencies

The following are the file dependencies that are needed to run this notebook:

Data

  • btcusdt_binance.csv : Public Trades
  • orderbooks_05jul21.json : Order Books

2.3 Install Packages

In [ ]:
import pandas as pd 
import json
from tkinter import filedialog as fd
from tkinter import *
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
pio.renderers.keys()
import pandas as pd
import functions as ft
import data as dt
import functions 
import visualizations
In [ ]:
 


3. Data Description


In [ ]:
# DataFrame Head
# Obtaining JSON file from data document 
data_ob = dt.ob_data
# Orderbook & Timestamp
ob_ts = list(data_ob.keys())
l_ts = [pd.to_datetime(i_ts) for i_ts in ob_ts]
# Metrics from Functions library in the other document
ob_df,_,_ = functions.df_metrics(data_ob)
# Midpricess 
midprices = ob_df["Mid Price"]
pd.DataFrame(midprices.head())
Out[ ]:
Mid Price
2021-07-05 13:06:46.571000+00:00 28272.5
2021-07-05 13:06:47.918000+00:00 28272.5
2021-07-05 13:06:49.414000+00:00 28272.5
2021-07-05 13:06:51.077000+00:00 28276.5
2021-07-05 13:06:52.426000+00:00 28276.5
In [ ]:
# Midprices Histogram
pd.DataFrame(midprices).hist(bins="auto");


4. Model 1: Asset Pricing Theory


In [ ]:
# Martingale results:
# Mid Prices from metrics
midprices = ob_df["Mid Price"]

# Asset pricing model
MA_e1 = functions.Model1_E1(midprices)
MA_e1
Out[ ]:
amount ratio
e1 1763.0 0.73
e2 637.0 0.27
total 2400.0 2400.00


Experiment 4.B every minute

In [ ]:
# Experiment B 
M1_EB_1,M1_EB_2 = functions.Model1_E2(midprices)
M1_EB_1.head()
Out[ ]:
e1 e2 total ratio1 ratio2
13:6 6 2 8 0.750000 0.250000
13:7 27 13 40 0.675000 0.325000
13:8 31 8 39 0.794872 0.205128
13:9 27 11 38 0.710526 0.289474
13:10 30 10 40 0.750000 0.250000
In [ ]:
M1_EB_2
Out[ ]:
Total trades E1 Ratio Mean E2 Ratio Mean
0 2400 0.743515 0.256485
In [ ]:
# Experiment C
M1_EC_1,M1_EC_2,M1_EC_3 =  functions.Model1_E3(ob_df)
M1_EC_1
Out[ ]:
amount ratio
e1 1622.0 0.68
e2 778.0 0.32
total 2400.0 2400.00
In [ ]:
M1_EC_2.head()
Out[ ]:
e1 e2 total ratio1 ratio2
13:6 6 2 8 0.750000 0.250000
13:7 27 13 40 0.675000 0.325000
13:8 26 13 39 0.666667 0.333333
13:9 26 12 38 0.684211 0.315789
13:10 27 13 40 0.675000 0.325000
In [ ]:
M2_1,M2_2 = functions.Model2(pd.DataFrame(midprices),ob_df)
M1_EC_3
Out[ ]:
Total trades E1 Ratio Mean E2 Ratio Mean
0 2400 0.686378 0.313622
In [ ]:
M2_1.head()
Out[ ]:
Spread (OB) Calculated Spread Bid Mid Ask Calc Bid Calc Ask
2021-07-05 13:06:46.571000+00:00 5.0 0.070044 28267.5 28272.5 28277.5 28272.429956 28272.570044
2021-07-05 13:06:47.918000+00:00 5.0 0.070044 28267.5 28272.5 28277.5 28272.429956 28272.570044
2021-07-05 13:06:49.414000+00:00 5.0 0.070044 28267.5 28272.5 28277.5 28272.429956 28272.570044
2021-07-05 13:06:51.077000+00:00 3.0 0.070044 28273.5 28276.5 28279.5 28276.429956 28276.570044
2021-07-05 13:06:52.426000+00:00 3.0 0.070044 28273.5 28276.5 28279.5 28276.429956 28276.570044
In [ ]:
M2_2
Out[ ]:
Spread Mean Spread Variance Calculated Spread Spread Difference
0 3.946272 6.021695 0.070044 3.876229
In [ ]:
M1_e1
Out[ ]:
amount ratio
e1 1763.0 0.73
e2 637.0 0.27
total 2400.0 2400.00
In [ ]:
ec_graph = visualizations.APT_graph_w(M1_E3_2)
ec_graph.show(renderer="notebook")

5. Model 2: Roll Model

Experiment 5.A

In [ ]:
roll_rA = visualizations.Model2_TS_observed(M2_1)
roll_rA.show(renderer = "notebook")

Experiment 5.B

In [ ]:
roll_rB = visualizations.Model2_TS_Theoretical(M2_1)
roll_rB.show(renderer = "notebook")

The theorical ask and the theorical bid it´s not usefull for the practice, and the predictions are useless.

References


  • Financial asset pricing theory: Claus Munk

  • SIMPLE NONPARAMETRIC ESTIMATORS FOR THE BID-ASK SPREAD IN THE ROLL MODEL By Xiaohong Chen, Oliver Linton, Stefan Schneeberger, and Yanping Yi

[1] Munnoz, 2020. Python project template. https://github.com/iffranciscome/python-project. (2021).